import matplotlib.pyplot as plt
import pandas
from openpyxl import load_workbook
import numpy as np
from pyecharts.charts import WordCloud

wb = load_workbook("豆瓣电影总.xlsx")
ws = wb["Sheet1"]

plt.rcParams['font.family'] = 'SimHei'  # 配置中文字体
plt.rcParams['font.size'] = 15   # 更改默认字体大小

# 各分数电影总数
score = []
for i in ws["B"]:
    score.append(i.value)
del score[0]

score = [str(i) for i in score]
score.sort()

plt.figure(figsize=(24, 6))
plt.subplot(1, 1, 1)
plt.hist(score,  bins=101)
plt.xlabel('score')
plt.ylabel('num')
plt.show()

# 评分与电影时长的关系图
time = []
score = []
for i in ws["B"]:
    score.append(i.value)
del score[0]
score = [float(i) for i in score]

for i in ws["G"]:
    time.append(i.value)
del time[0]
time = [int(i) for i in time]
# 剔除异常数据
for i in range(len(time)):
    try:
        if time[i] < 30 or time[i] > 300:
            del time[i]
            del score[i]
    except:
        break
plt.figure(figsize=(12, 12))
plt.scatter(time, score)
plt.xlabel('time')
plt.ylabel('score')
plt.show()

# 评论人数与分数的关系图
comment = []
score = []
for i in ws["B"]:
    score.append(i.value)
del score[0]
score = [float(i) for i in score]

for i in ws["I"]:
    comment.append(i.value)
del comment[0]
comment = [int(i) for i in comment]

plt.figure(figsize=(14, 6))
plt.scatter(comment, score)
plt.xlabel('comment')
plt.ylabel('score')
plt.show()

# 上映年份与分数的关系图
year = []
score = []
for i in ws["B"]:
    score.append(i.value)
del score[0]
score = [float(i) for i in score]

for i in ws["E"]:
    year.append(i.value)
del year[0]
year = [int(i) for i in year]
print(year)

plt.figure(figsize=(14, 6))
plt.scatter(year, score)
plt.xlabel('year')
plt.ylabel('score')
plt.show()

# 上映地区影片数量
area = []
for i in ws["J"]:
    area.append(i.value)
del area[0]

area = [str(i) for i in area]

plt.figure(figsize=(12, 6))
plt.hist(area,  bins=100)
plt.xlabel('area')
plt.ylabel('num')
plt.xticks(rotation=-270)
plt.show()

# 类型影片数量，影片类型与分数的关系图
type = []
for i in ws["F"]:
    type.append(i.value)
del type[0]

type = [str(i) for i in type]
plt.figure(figsize=(12, 6))
plt.hist(type,  bins=50)
plt.xlabel('type')
plt.ylabel('num')
plt.xticks(rotation=-270)
plt.show()

score = []
for i in ws["B"]:
    score.append(i.value)
del score[0]
score = [float(i) for i in score]

plt.figure(figsize=(12, 6))
plt.scatter(type, score)
plt.xlabel('score')
plt.ylabel('type')
plt.xticks(rotation=-270)
plt.show()

# 制作词云
director = []
for i in ws["C"]:
    director.append(i.value)
del director[0]
director = [str(i) for i in director]

actor = []
for i in ws["D"]:
    actor.append(i.value)
del actor[0]
actor = [str(i) for i in actor]

data1 = type + director + actor + area
data = data1[0]
for i in data1:
    data = data + ',' + i
WC = WordCloud(font_path='C:/Windows/Fonts/STKAITI.TTF', background_color='white',
               width=5000, height=3000, margin=2).generate(data)
plt.figure(figsize=(16, 8))
plt.imshow(WC)
plt.axis('off')
plt.show()
